import Vue from 'vue'
import Vuex from 'vuex'
import { getUsersMenuList, getUsersInfoApi} from  '@/api/user'
import { listToTree } from '@/utils/util'
// default router permission control
// import permission from './modules/permission'

// dynamic router permission control (Experimental)

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
  },
  state: {
    token: '',
    menuList: [],
    role_key: null, // 角色id
  },
  mutations: {},
  actions: {
    // 获取用户菜单
    async getUsersMenu({state}) {
     return new Promise(async (resolve,reject) => {
      const {data} = await getUsersMenuList()
      for (let i = 0; i < data.length; i++) {
        data[i]['path'] = data[i].url_path
      }
      // 转树形
      const threeList = listToTree(data, 0)
      state.menuList = threeList
      resolve(threeList)
     })
    },
    // 获取登录用户信息
    async getUsersInfo({state}) {
      const { data } = await getUsersInfoApi()
      state.role_key = data.role_key
    }
  }
})
